package UPPM;

import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestAdvancedSearch {
	WebDriver driver = new FirefoxDriver();

	@Before
	public void setUp() throws Exception {
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		ResourceBundle bundle = ResourceBundle.getBundle("values");
		driver.get(bundle.getString("test_link"));
//		Utils.acceptCookies(driver);
		driver.get(bundle.getString("test_link2"));
	}

	@Test
	public void testAdvancedSearchTabInitialState() throws Exception {
		System.out.println("testAdvancedSearchTabInitialState");
		System.getProperty("line.separator");
		WebElement advSearch = driver.findElement(By.id("btn_advsearch"));
		Assert.assertEquals("Advanced Search", advSearch.getText());
		advSearch.click();
         
		Thread.sleep(5000L); 
		
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("categories_and_attributes"))); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("your_selection")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("additional_filters")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("lyrics")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("composer")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("tracklength")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("advanced_search_counter")));		
		
		//check categories
		WebElement music_for= driver.findElement(By.xpath("//*[@id='categories']/li[1]/a/span"));
		Assert.assertEquals("Music For", music_for.getText());
		WebElement music_styles= driver.findElement(By.xpath("//*[@id='categories']/li[2]/a/span"));
		Assert.assertEquals("Music Styles", music_styles.getText());
		WebElement moods= driver.findElement(By.xpath("//*[@id='categories']/li[3]/a/span"));
		Assert.assertEquals("Moods", moods.getText());
		WebElement instruments= driver.findElement(By.xpath("//*[@id='categories']/li[4]/a/span"));
		Assert.assertEquals("Instruments", instruments.getText());
		WebElement tempos= driver.findElement(By.xpath("//*[@id='categories']/li[5]/a/span"));
		Assert.assertEquals("Tempos", tempos.getText());
		WebElement eras= driver.findElement(By.xpath("//*[@id='categories']/li[6]/a/span"));
		Assert.assertEquals("Eras", eras.getText());
		WebElement country= driver.findElement(By.xpath("//*[@id='categories']/li[7]/a/span"));
		Assert.assertEquals("Country List", country.getText());
		WebElement tunes= driver.findElement(By.xpath("//*[@id='categories']/li[8]/a/span"));
		Assert.assertEquals("Well Known Tunes", tunes.getText());
		
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("adv_search_btn")));			
	}

	@Test
	public void testAdvancedSearchItemsFound() throws Exception {

		WebElement advSearch = driver.findElement(By.id("btn_advsearch"));
		advSearch.click();

		Utils.waitForElement(driver,By.xpath("//*[@id='adv_search']/form/div"));
					
		WebElement musicStyles = driver.findElement(By.xpath("//*[@id='categories']/li[2]/a/span"));
        musicStyles.click();
		
		Utils.waitForElement(driver,By.xpath("//div[@id='selectlist_1']/div/div/a/span"));
			
		WebElement addBtn = driver.findElement(By.xpath("//*[@id='selectlist_1']/div/div[1]/div/a"));
		addBtn.click();
		
		Thread.sleep(3000);
		WebElement advSearchBtn = driver.findElement(By.id("adv_search_btn"));
		advSearchBtn.click();
		
		Thread.sleep(10000L); 
		Utils.waitForElement(driver,By.id("filters"));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.cssSelector("span.title")));

	}
	@Test
	public void testAdvancedSearchNoItemsFound() throws Exception {
		System.out.println("testAdvancedSearchNoItemsFound");
		System.getProperty("line.separator");
		WebElement advSearch = driver.findElement(By.id("btn_advsearch"));
		advSearch.click();

		Utils.waitForElement(driver,By.xpath("//*[@id='adv_search']/form/div"));
					
		WebElement musicStyles = driver.findElement(By.xpath("//*[@id='categories']/li[2]/a/span"));
        musicStyles.click();
			
		Utils.waitForElement(driver,By.xpath("//div[@id='selectlist_1']/div/div/a/span"));
				
		WebElement addBtn = driver.findElement(By.xpath("//*[@id='selectlist_1']/div/div[1]/div/a"));
		addBtn.click();
			
		Thread.sleep(3000);	
		WebElement lyrics= driver.findElement(By.id("lyrics"));
		lyrics.sendKeys("testinggggg");
		WebElement composer= driver.findElement(By.id("composer"));
		composer.sendKeys(" ");
		
		Thread.sleep(10000L); 
		
		Utils.waitForElement(driver,By.id("error_overlay"));
		WebElement no_results_msg= driver.findElement(By.id("error_overlay"));
//		Assert.assertEquals(true, no_results_msg.isDisplayed());
	}
	
	@Test
	public void testAdvancedSearchItemsFoundUserLoggedIn() throws Exception {
        
		Utils.Login(driver);
		System.out.println("testAdvancedSearchItemsFoundUserLoggedIn");
		System.getProperty("line.separator");
		WebElement advSearch = driver.findElement(By.id("btn_advsearch"));
		advSearch.click();

		Utils.waitForElement(driver,By.xpath("//*[@id='adv_search']/form/div"));
					
		WebElement musicStyles = driver.findElement(By.xpath("//*[@id='categories']/li[2]/a/span"));
        musicStyles.click();
		
		Utils.waitForElement(driver,By.xpath("//div[@id='selectlist_1']/div/div/a/span"));
			
		WebElement addBtn = driver.findElement(By.xpath("//*[@id='selectlist_1']/div/div[1]/div/a"));
		addBtn.click();
		
		Thread.sleep(3000);
		WebElement advSearchBtn = driver.findElement(By.id("adv_search_btn"));
		advSearchBtn.click();
		Thread.sleep(3000);
		Thread.sleep(10000L); 
		Utils.waitForElement(driver,By.id("filters"));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.cssSelector("span.title")));

	}
	
	@Test
	public void testAdvancedSearchNoItemsFoundUserLoggedIn() throws Exception {

		Utils.Login(driver);
		System.out.println("testAdvancedSearchNoItemsFoundUserLoggedIn");
		System.getProperty("line.separator");
		
		WebElement advSearch = driver.findElement(By.id("btn_advsearch"));
		advSearch.click();

		Utils.waitForElement(driver,By.xpath("//*[@id='adv_search']/form/div"));
					
		WebElement musicStyles = driver.findElement(By.xpath("//*[@id='categories']/li[2]/a/span"));
        musicStyles.click();
			
		Utils.waitForElement(driver,By.xpath("//div[@id='selectlist_1']/div/div/a/span"));
				
		WebElement addBtn = driver.findElement(By.xpath("//*[@id='selectlist_1']/div/div[1]/div/a"));
		addBtn.click();
			
		Thread.sleep(3000);		
		WebElement lyrics= driver.findElement(By.id("lyrics"));
		lyrics.sendKeys("testinggggg");
		WebElement composer= driver.findElement(By.id("composer"));
		composer.sendKeys(" ");
		
		Thread.sleep(10000L); 
		
		Utils.waitForElement(driver,By.id("error_overlay"));
		WebElement no_results_msg= driver.findElement(By.id("error_overlay"));
//		Assert.assertEquals(true, no_results_msg.isDisplayed());
	}	


	

	@After
	public void tearDown() throws Exception {
		driver.quit();
	}
}
